package com.example.beat_x3.Adapter;


import android.annotation.SuppressLint;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.beat_x3.Client.LoadImages;
import com.example.beat_x3.R;

import java.util.ArrayList;
import java.util.Random;

import Entity.Inf;
import Entity.Place;

public class VenueAdapter extends RecyclerView.Adapter<VenueAdapter.Holder> {
    public ArrayList<Place> places;
    private OnItemClickListener mOnItemClickListener;
    Random r = new Random();

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(String placeNumber, String placeImageSrc);
    }

    @NonNull
    @Override
    public VenueAdapter.Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new VenueAdapter.Holder(LayoutInflater.from(Inf.context).inflate(R.layout.item_venue, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull VenueAdapter.Holder holder, int position) {
        holder.bind(position);
    }

    @Override
    public int getItemCount() {
        if (places == null) return 0;
        return places.size();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void refresh() {
        if (places != null)
            notifyDataSetChanged();

    }

    class Holder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView place;
        String placeId;
        String placeImageSrc;

        public Holder(@NonNull View itemView) {
            super(itemView);
            this.imageView = itemView.findViewById(R.id.v1_image);
            this.place = itemView.findViewById(R.id.v1_text2);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(placeId, placeImageSrc);
                    }
                }
            });
        }

        void bind(int position) {
            if (places != null) {
                placeId = places.get(position).PlaceNumber;
                placeImageSrc = places.get(position).PlaceImage;
                place.setText(places.get(position).PlacePosition);
                ViewGroup.LayoutParams params = imageView.getLayoutParams();
                params.height = 200 + r.nextInt(200);
                imageView.setLayoutParams(params);
                if (Inf.place_images != null) {
                    if (!Inf.place_images.containsKey(placeImageSrc)) {
                        LoadImages.downloadImages(LoadImages.PLACE_SRC, placeImageSrc);
                    }
                    imageView.setImageURI(Uri.parse(LoadImages.PLACE_SRC + "/" + placeImageSrc));
                }
            }
        }
    }
}
